////////////////////////////////////////////////////////////////////////////////
/// @file    main.c
/// @author  AE TEAM
/// @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _MAIN_C_

// Files includes
#include "main.h"
#include <lwip/opt.h>
#include <lwip/arch.h>
#include "tcpip.h"
#include "lwip/init.h"
#include "lwip/netif.h"
#include "ethernetif.h"
#include "netif/ethernet.h"
#include "lwip/def.h"
#include "lwip/stats.h"
#include "lwip/etharp.h"
#include "lwip/ip.h"
#include "lwip/snmp.h"
#include "lwip/timeouts.h"
#include "delay.h"
#include "eth_bsp_config.h"
//#include "tcpclient.h"
#include "tcpecho.h"
#include "uart.h"
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Example_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MAIN
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MAIN_Exported_Constants
/// @{


////////////////////////////////////////////////////////////////////////////////
/// @brief  This function is main entrance.
/// @param  None.
/// @retval  0.
////////////////////////////////////////////////////////////////////////////////
ip4_addr_t ipaddr;
ip4_addr_t netmask;
ip4_addr_t gw;

struct netif gnetif;

bool ethernet_flag;

void lwIP_Init(void)
{
#ifdef USE_DHCP
    ip_addr_set_zero_ip4(&ipaddr);
    ip_addr_set_zero_ip4(&netmask);
    ip_addr_set_zero_ip4(&gw);
#else
    IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
    IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
    IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
#endif

    lwip_init();

    netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);

    netif_set_default(&gnetif);

    if (netif_is_link_up(&gnetif)) {
        netif_set_up(&gnetif);
    }
    else {
        netif_set_down(&gnetif);
    }
}

u32_t sys_now(void)
{
    return SystemTick_Count;
}
void SysTick_Handler(void)
{
    SystemTick_Count += 10;
}
void ETH_IRQHandler()
{
    if (ETH_GetDMAITStatus(ETH_DMA_IT_R)) {                         // Frame received
        /* Receive complete callback */
        // callback
        ethernet_flag = true;

        ethernetif_input(&gnetif);

        /* Clear the Eth DMA Rx IT pending bits */
        ETH_DMAClearITPendingBit(ETH_DMA_IT_R);

    }
    else if (ETH_GetDMAITStatus(ETH_DMA_IT_T)) {                    // Frame transmitted
        // Transfer complete callback
        // callback

        // Clear the Eth DMA Tx IT pending bits
        ETH_DMAClearITPendingBit(ETH_DMA_IT_T);
    }

    // Clear the interrupt flags
    ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);

    // ETH DMA Error
    if(ETH_GetDMAITStatus(ETH_DMA_FLAG_AIS)) {
        // Ethernet Error callback
        // callback

        // Clear the interrupt flags
        ETH_DMAClearITPendingBit(ETH_DMA_FLAG_AIS);
    }
}

int main(void)
{
    SystemTick_Count = 0;

    ethernet_flag = false;
    CONSOLE_Init(115200);
    lwIP_Init();

    TCP_Echo_Init();

#ifdef USE_DHCP
    dhcp_pre_check(&gnetif);
#endif

    while(1) {

#ifdef USE_DHCP
        dhcp_tick_check(&gnetif, SystemTick_Count);
#endif
        sys_check_timeouts();
    }
}



/// @}

/// @}

/// @}
